package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.h3c.ptability.bo.SendEmailBO;
import com.h3c.ptability.dto.BdPostDTO;
import com.h3c.ptability.dto.SapPostDTO;
import com.h3c.ptability.entity.BdPost;
import com.h3c.ptability.service.IBdPostService;
import com.h3c.ptability.support.ResultData;
import com.h3c.ptability.utils.EmailUtil;
import com.h3c.ptability.vo.BdPostVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 岗位表(BdPost)表控制层
 *
 * @author makejava
 * @since 2022-06-24 17:45:07
 */
@Slf4j
@RestController
@RequestMapping("bdPost")
public class BdPostController {
    /**
     * 服务对象
     */
    @Resource
    private IBdPostService bdPostService;

    /**
     * 根据postId返回岗位名称（feign调用）
     *
     * @param postId postName 岗位Id
     * @return 返回岗位
     */
    @GetMapping("/getPostNameByPostId")
    public String getPostNameByPostId(@RequestParam String postId) {
        BdPost bdPost = bdPostService.getById(postId);
        if(Objects.isNull(bdPost)){
            return "";
        }
        return bdPost.getPostName();
    }

    @GetMapping("/getPostByPostId")
    public BdPost getPostByPostId(@RequestParam String postId) {
        BdPost bdPost = bdPostService.getById(postId);
        if(Objects.isNull(bdPost)){
            return new BdPost();
        }
        return bdPost;
    }

    /**
     * 查询岗位
     *
     * @param post postName 岗位名称
     * @return 返回岗位
     */
    @GetMapping("/searchBdPost")
    public ResultData<IPage<BdPostVO>> searchBdPost(@Valid @ModelAttribute BdPostDTO post) {
        try {
            IPage<BdPostVO> iPage = bdPostService.searchBdPost(post);
            return ResultData.data(iPage);
        } catch (Exception e) {
            log.error("查询异常：", e);
            return ResultData.fail("查询失败:{}", e.getMessage());
        }
    }

    /**
     * 查询岗位
     *
     * @param post postName 岗位名称
     * @return 返回岗位
     */
    @GetMapping("/searchPost")
    public ResultData<IPage<BdPostVO>> searchPost(@Valid @ModelAttribute SapPostDTO post) {
        try {
            IPage<BdPostVO> iPage = bdPostService.searchPost(post);
            return ResultData.data(iPage);
        } catch (Exception e) {
            log.error("查询异常：", e);
            return ResultData.fail("查询失败:{}", e.getMessage());
        }
    }

    @PostMapping("/syncPostJob")
    public ResultData<String> syncPostJob() {
        try {
            bdPostService.syncPostJob();
            return ResultData.success("同步成功");
        } catch (Exception e) {
            log.error("查询异常：", e);
            return ResultData.fail("查询失败:{}", e.getMessage());
        }
    }
}

